package com.byx.chatclient.model;

import android.text.TextUtils;
import android.util.Log;

import com.byx.chatclient.config.ServerProperties;
import com.byx.chatclient.contract.user.RegisterTaskContract;
import com.byx.chatclient.entity.Result;
import com.byx.chatclient.entity.ResultCode;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import okhttp3.Call;
import okhttp3.Response;

/**
 * RegisterModel
 *
 * @author Superb
 * @date 2021/3/23 11:12
 */
public class RegisterModel implements RegisterTaskContract.Model {
    public static final String TAG = RegisterModel.class.getSimpleName();

    private static final int PASSWORD_MIN_LENGTH = 8;
    private static final int PASSWORD_MAX_LENGTH = 16;

    /**
     * 检验参数是否合法
     *
     * @param number
     * @return
     */
    @Override
    public void checkNumber(String number, SimpleCallBack simpleCallBack) {
        String exampleEmail = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9]+(\\.[a-zA-Z0-9]+)+$";
        String exampleTel="^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8}$";
        if (TextUtils.isEmpty(number)){
            simpleCallBack.onError("手机号/邮箱不能为空！");
        }else if (!number.matches(exampleEmail) && !number.matches(exampleTel)) {
            simpleCallBack.onError("手机号/邮箱格式不正确！");
        }else {
            simpleCallBack.onSuccess();
        }
    }

    /**
     * 检验参数是否合法
     *
     * @param number
     * @param code
     * @param password
     * @param simpleCallBack
     * @return
     */
    @Override
    public void checkParam(String number, String code, String password, SimpleCallBack simpleCallBack) {
        String exampleEmail = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9]+(\\.[a-zA-Z0-9]+)+$";
        String exampleTel="^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8}$";
        String exampleCode="^\\d{6}$";
        if (TextUtils.isEmpty(number)){
            simpleCallBack.onError("手机号/邮箱不能为空！");
        }else if (!number.matches(exampleEmail) && !number.matches(exampleTel)) {
            simpleCallBack.onError("手机号/邮箱格式不正确！");
        }else if (TextUtils.isEmpty(code)){
            simpleCallBack.onError("验证码不能为空！");
        }else if (!code.matches(exampleCode)){
            simpleCallBack.onError("验证码格式不正确！");
        }else if (TextUtils.isEmpty(password)){
            simpleCallBack.onError("密码不能为空!");
        }else if (password.length()<PASSWORD_MIN_LENGTH || password.length()>PASSWORD_MAX_LENGTH){
            simpleCallBack.onError("密码长度不符合要求！");
        }else {
            simpleCallBack.onSuccess();
        }
    }

    /**
     * 获取验证码
     *
     * @param number
     * @param callBack
     */
    @Override
    public void getVerificationCode(String number, SimpleCallBack callBack) {
        int type = 200;
        OkHttpUtils.get().url(ServerProperties.WEB_SERVER_URL+"user/sendVerificationCode")
                .addParams("number",number)
                .addParams("type", String.valueOf(type))
                .build()
                .execute(new Callback<Result>() {
                    /**
                     * Thread Pool Thread
                     *
                     * @param response
                     * @param id
                     */
                    @Override
                    public Result parseNetworkResponse(Response response, int id) throws Exception {
                        return new Gson().fromJson(response.body().string(),Result.class);
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                        Log.e(TAG, "onError: "+e.getCause());
                    }

                    @Override
                    public void onResponse(Result response, int id) {
                        if(null == response){
                            callBack.onError(null);
                            return;
                        }
                        if(response.getCode() == Result.SUCCESS){
                            //发送成功
                            callBack.onSuccess();
                        }else {
                            //发送失败
                            callBack.onError(response.getMessage());
                        }
                    }
                });
    }

    /**
     * 注册逻辑
     *
     * @param number
     * @param verificationCode
     * @param password
     * @param registerWay
     * @param callback         回调
     */
    @Override
    public void register(String number, String verificationCode, String password, int registerWay, RegisterCallBack<Result> callback) {
        OkHttpUtils.post().url(ServerProperties.WEB_SERVER_URL+"user/register")
                .addParams("number",number)
                .addParams("verificationCode",verificationCode)
                .addParams("password",password)
                .addParams("registerWay", String.valueOf(registerWay))
                .build()
                .execute(new Callback<Result>() {
                    /**
                     * Thread Pool Thread
                     *
                     * @param response
                     * @param id
                     */
                    @Override
                    public Result parseNetworkResponse(Response response, int id) throws Exception {
                        return new Gson().fromJson(response.body().string(),Result.class);
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                        Log.e(TAG, "onError: "+e.getCause());
                    }

                    @Override
                    public void onResponse(Result response, int id) {
                        if(null == response){
                            callback.onFailure(response);
                            return;
                        }
                        if(response.getCode().equals(Result.SUCCESS)){
                            //注册成功
                            callback.onSuccess(response);
                        }else {
                            //注册失败
//                            if(resultCode.equals(ResultCode.USER_HAS_EXISTED)){
//                                callback.onAccountExists();
//                            }else {
//                                callback.onFailure(response);
//                            }
                        }
                    }
                });
    }
}
